package com.building.service.impl;

import com.building.dao.BuildingDao;
import com.building.entity.Admin;
import com.building.entity.Building;
import com.building.entity.DormitoryManager;
import com.building.dao.DormitoryManagerDao;
import com.building.response.ServerResponse;
import com.building.service.DormitoryManagerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (DormitoryManager)表服务实现类
 *
 * @author TianCai
 * @since 2023-01-14 15:07:26
 */
@Service("dormitoryManagerService")
public class DormitoryManagerServiceImpl implements DormitoryManagerService {
    @Resource
    private DormitoryManagerDao dormitoryManagerDao;
    @Resource
    private BuildingDao buildingDao;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public DormitoryManager queryById(Integer id) {
        return this.dormitoryManagerDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param dormitoryManager 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<DormitoryManager> queryByPage(DormitoryManager dormitoryManager, PageRequest pageRequest) {
        long total = this.dormitoryManagerDao.count(dormitoryManager);
        return new PageImpl<>(this.dormitoryManagerDao.queryAllByLimit(dormitoryManager, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param dormitoryManager 实例对象
     * @return 实例对象
     */
    @Override
    public DormitoryManager insert(DormitoryManager dormitoryManager) {
        this.dormitoryManagerDao.insert(dormitoryManager);
        return dormitoryManager;
    }

    /**
     * 修改数据
     *
     * @param dormitoryManager 实例对象
     * @return 实例对象
     */
    @Override
    public DormitoryManager update(DormitoryManager dormitoryManager) {
        this.dormitoryManagerDao.update(dormitoryManager);
        return this.queryById(dormitoryManager.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.dormitoryManagerDao.deleteById(id) > 0;
    }

    @Override
    public ServerResponse<PageInfo<DormitoryManager>> getPageInfoByAccount(Integer pageNum, Integer pageSize, String account) {
        PageHelper.startPage(pageNum, pageSize);
        List<DormitoryManager> dormitoryManagers = this.dormitoryManagerDao.queryAllByAccount(account);
        PageInfo<DormitoryManager> dormitoryManagerPageInfo = new PageInfo<>(dormitoryManagers);
        if (dormitoryManagerPageInfo.getList().size()==0) return ServerResponse.createByErrorMessage("暂无数据!换个关键词试试吧!");
        return ServerResponse.createBySuccess("查询成功,共"+dormitoryManagerPageInfo.getList().size()+"条数据",dormitoryManagerPageInfo);
    }

    @Override
    public ServerResponse<PageInfo<DormitoryManager>> getPageInfo(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<DormitoryManager> dormitoryManagers = this.dormitoryManagerDao.queryAll();
        PageInfo<DormitoryManager> dormitoryManagerPageInfo = new PageInfo<>(dormitoryManagers);
        return ServerResponse.createBySuccess("获取分页数据成功!", dormitoryManagerPageInfo);
    }

    @Override
    public ServerResponse<PageInfo<DormitoryManager>> getPageInfoByName(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum, pageSize);
        List<DormitoryManager> dormitoryManagers = this.dormitoryManagerDao.queryAllByName(name);
        PageInfo<DormitoryManager> dormitoryManagerPageInfo = new PageInfo<>(dormitoryManagers);
        if (dormitoryManagerPageInfo.getList().size()==0) return ServerResponse.createByErrorMessage("暂无数据!换个关键词试试吧!");
        return ServerResponse.createBySuccess("查询成功,共"+dormitoryManagerPageInfo.getList().size()+"条数据",dormitoryManagerPageInfo);
    }

    @Override
    public ServerResponse<DormitoryManager> addDorManager(String name, String number, String account, String password, Integer sex) {
        if (name.equals("") || account.equals("") || password.equals("")) return ServerResponse.createByErrorMessage("信息输入不完整!");
        if(dormitoryManagerDao.queryByAccount(account)!=null) return ServerResponse.createByErrorMessage("此账户名太受欢迎了换一个试试吧!");
        if(dormitoryManagerDao.queryAllByNumber(number)!=null) return ServerResponse.createByErrorMessage("此宿管编号已存在!");
        DormitoryManager dormitoryManager = new DormitoryManager(null,name,account,password,sex,0,number,new Date(),new Date(),1,true);
//        dormitoryManager.setName(name);
//        dormitoryManager.setDManagerNum(number);
//        dormitoryManager.setAccount(account);
//        dormitoryManager.setPassword(password);
//        dormitoryManager.setSex(sex);
//        dormitoryManager.setCreateTime(new Date());
//        dormitoryManager.setUpdateTime(new Date());
//        dormitoryManager.setScore(0);
//        dormitoryManager.setStatus(true);
//        dormitoryManager.setVersion(1);
        int insert = dormitoryManagerDao.insert(dormitoryManager);
        if (insert!=1) return  ServerResponse.createByErrorMessage("数据添加失败,请检查服务器!");
        return ServerResponse.createBySuccess("添加数据成功!",dormitoryManager);
    }

    @Override
    public ServerResponse<DormitoryManager> updateDorManager(Integer id, String name, String number, String account, String password, Integer sex) {
        DormitoryManager dormitoryManager = dormitoryManagerDao.queryById(id);
        if (dormitoryManager==null) return ServerResponse.createByErrorMessage("修改失败不存在这条数据");
        List<DormitoryManager> dormitoryManagers = dormitoryManagerDao.queryAll();
        int i = dormitoryManagers.indexOf(dormitoryManager);
        for (int count = 0;count< dormitoryManagers.size();count++){
            if (count==i) continue;//把自己去掉之后看看有没有别的人和我用同样的账户名
            if (number.equals(dormitoryManagers.get(count).getDManagerNum())) return ServerResponse.createByErrorMessage("此宿管编号已经存在!");
            if (account.equals(dormitoryManagers.get(count).getAccount())) return ServerResponse.createByErrorMessage("此账户名太受欢迎了换一个试试吧!");
        }
        dormitoryManager.setName(name);
        dormitoryManager.setSex(sex);
        dormitoryManager.setDManagerNum(number);
        dormitoryManager.setAccount(account);
        dormitoryManager.setPassword(password);
        dormitoryManager.setUpdateTime(new Date());
        dormitoryManager.setVersion(dormitoryManager.getVersion()+1);
        int update = dormitoryManagerDao.update(dormitoryManager);
        if (update!=1) return  ServerResponse.createByErrorMessage("数据修改失败,请检查服务器!");
        return ServerResponse.createBySuccess("修改数据成功!",dormitoryManager);
    }

    @Override
    public ServerResponse<DormitoryManager> deleteDroManager(Integer id) {
        DormitoryManager dormitoryManager = this.dormitoryManagerDao.queryById(id);
        if (dormitoryManager == null) return ServerResponse.createByErrorMessage("此id对应的用户不存在");
        dormitoryManager.setStatus(false);
        dormitoryManager.setUpdateTime(new Date());
        dormitoryManager.setVersion(dormitoryManager.getVersion()+1);
        int update = dormitoryManagerDao.update(dormitoryManager);
        if (update!=1) return  ServerResponse.createByErrorMessage("数据删除失败,请检查服务器!");
        return ServerResponse.createBySuccess("删除用户成功!",dormitoryManager);
    }

    @Override
    public ServerResponse<Integer> getCount() {
        return ServerResponse.createBySuccess("宿管总人数!",this.dormitoryManagerDao.queryAllByStatus());
    }

    @Override
    public ServerResponse<Integer> getscore(Integer mid) {
        DormitoryManager dormitoryManager = this.dormitoryManagerDao.queryById(mid);
        return ServerResponse.createBySuccess("获取分数成功！",dormitoryManager.getScore());
    }

    @Override
    public ServerResponse<Building> changeManager(Integer mid,Integer id) {
        Building building = this.buildingDao.queryById(id);
        if (building == null) return ServerResponse.createByErrorMessage("此id对应的楼宇不存在");
        DormitoryManager dormitoryManager = this.dormitoryManagerDao.queryById(mid);
        if (dormitoryManager == null) return ServerResponse.createByErrorMessage("此id对应的宿管不存在");
        if (building.getDMId()!=null)  return ServerResponse.createByErrorMessage("已分配宿管，换别的楼宇试试吧");
        building.setDMId(mid);
        building.setVersion(building.getVersion()+1);
        building.setUpdateTime(new Date());
        int update = buildingDao.update(building);
        if (update!=1) return  ServerResponse.createByErrorMessage("请检查服务器!");
        return ServerResponse.createBySuccess("添加宿管成功!",building);
    }
}